package com.controller;

import com.pojo.Goods;
import com.pojo.Member;
import com.pojo.Provider;
import com.pojo.User;
import com.service.UpdateService;
import jdk.internal.org.objectweb.asm.tree.IincInsnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
    @RequestMapping("update")
    public class UpdateController {
        @Autowired
        private UpdateService updateService;
        /*
        * 加载修改用户界面，
        * 查询需要修改的用户信息
        * 修改用户信息
        */
        @RequestMapping("updateUserLoad")
        public ModelAndView updateUser(Integer id){
            User user = updateService.getUserById(id);
            ModelAndView mv=new ModelAndView("update/updateuser");
            mv.addObject("user",user);
            return mv;
        }
        @RequestMapping(value = "updateUser",produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String updateUser(Integer id,String username,String password,String phone,String  address,Integer gender,Integer role,String  birthday){
            User user = new User();
            user.setId(id);
            user.setUserName(username);
            user.setUserPassword(password);
            user.setPhone(phone);
            user.setAddress(address);
            user.setGender(gender);
            user.setUserRole(role);
            user.setBirthday(birthday);
            int i = updateService.updateUser(user);
            if (i>0){
                return "修改成功，是否返回用户列表";
            }
            return "修改失败，是否返回用户列表";
        }




        /**
         * 1.加载修改供应商界面，并根据id查找对应的供应商
         * 2.查询id对应的供应商信息
         * 3.修改供应商
        **/
        @RequestMapping("updateProviderLoad")
        public ModelAndView updateProviderLoad(Integer id){
            Provider providerById = updateService.getProviderById(id);
            ModelAndView mv = new ModelAndView("update/updateprovider");
            mv.addObject("providerById",providerById);
            return mv;
        }
        @RequestMapping(value = "updateProvider",produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String updateProvider(Integer id,String proName,String proCode,String proContact,String proPhone,String proAddress,String proDesc){
            Provider provider = new Provider();
            provider.setId(id);
            provider.setProName(proName);
            provider.setProCode(proCode);
            provider.setProContact(proContact);
            provider.setProPhone(proPhone);
            provider.setProAddress(proAddress);
            provider.setProDesc(proDesc);
            int i = updateService.updateProvider(provider);
            if (i>0){
                return "修改成功，是否返回";
            }
            return "修改失败，是否返回";
        }


        /**
         * 1.加载修改商品界面，并传入需要修改的商品id
         * 2.查询id对应的商品信息
         * 3.修改商品
         **/
        @RequestMapping("updateGoodsLoad")
        public ModelAndView updateGoodsLoad(Integer id){
            Goods goodsById = updateService.getGoodsById(id);
            List<Provider> providerToUpdateGoods = updateService.getProviderToUpdateGoods();
            ModelAndView mv = new ModelAndView("update/updategoods");
            mv.addObject("goodsById",goodsById);
            mv.addObject("providerToUpdateGoods",providerToUpdateGoods);
            return mv;
        }
        @RequestMapping(value = "updateGoods",produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String updateGoods(Integer id,String goodsCode,String goodsName,String goodsUnit, String goodsGenre,Integer goodsBid,Integer goodsPric,Integer  provider){
        Goods goods = new Goods();
        goods.setId(id);
        goods.setGoodsCode(goodsCode);
        goods.setGoodsName(goodsName);
        goods.setGoodsUnit(goodsUnit);
        goods.setGoodsGenre(goodsGenre);
        goods.setGoodsBid(goodsBid);
        goods.setGoodsPric(goodsPric);
        goods.setProviderid(provider);
            int i = updateService.updateGoods(goods);
            if (i>0){
                return "修改成功，是否返回";
            }
            return "修改失败，是否返回";
        }

        /**
         * 更新商品库存+
         **/
        @RequestMapping(value = "goodscount",produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String goodsCount(Integer id,Integer goodsCount){
            Goods goods = new Goods();
            goods.setId(id);
            goods.setGoodsCount(goodsCount);
            int i = updateService.updateGoodsCount(goods);
            if (i>0){
                return "进货成功";
            }
            return "进货失败";
        }
    /**
     * 更新商品库存-
     **/
    @RequestMapping(value = "goodsreturn",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String goodsCount1(Integer id,Integer goodsreturn){
        Goods goods = new Goods();
        goods.setId(id);
        goods.setGoodsreturn(goodsreturn);
        int i = updateService.updateGoodsCount1(goods);
        if (i>0){
            return "退货成功";
        }
        return "退货失败";
    }

    /**
     * 1.加载修改会员界面，并传入需要修改的商品id
     * 2.查询id对应的会员信息
     * 3.修改会员
     **/
    @RequestMapping("updateMemberLoad")
    public ModelAndView updateMemberLoad(Integer id){
        Member memberById = updateService.getMemberById(id);
        ModelAndView mv = new ModelAndView("update/updatemember");
        mv.addObject("memberById",memberById);
        return mv;
    }
    @RequestMapping(value = "updateMember",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String updateMember(Integer id,String name, Double discount, String phone, String address, String gender, String code, String birthday){
        Member member = new Member();
        member.setId(id);
        member.setMemberBirthday(birthday);
        member.setMemberCode(code);
        member.setMemberName(name);
        member.setMenberDiscount(discount);
        member.setMenmberAddress(address);
        member.setMenberGender(gender);
        member.setMenmberPhone(phone);
        int i = updateService.updateMember(member);
        if (i>0){
            return "修改成功，是否返回";
        }
        return "修改失败，是否返回";
    }
    }
